﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FSAPI
{
    public class ClassGlobal
    {
        public RemoteServer rs = null;
        public MainForm  apiApp = null;
        public ClassGlobal()
        {
            apiApp = new MainForm();
            apiApp.Visible = true;
            apiApp.Show();
            apiApp.OnResultReturn += apiApp_OnResultReturn;
            apiApp.OnToHostMsgReturn +=apiApp_OnToHostMsgReturn;
            //apiApp.OnToHostSendPic += apiApp_OnToHostSendPic;
            init();
        }

        private void apiApp_OnToHostSendPic(object sender, EventArgs e)
        {
            if (rs != null)
            {
                rs.InspectionInstance.ReturnSendPicToHost(sender as byte[]);
            }
        }

        private void apiApp_OnToHostMsgReturn(object sender, EventArgs e)
        {
            if (rs != null)
            {
                rs.InspectionInstance.ReturnBackMsg (sender as string);
            }
        }

        private void apiApp_OnResultReturn(object sender, EventArgs e)
        {
            if (rs != null)
            {
                rs.InspectionInstance.ReturnResult(sender as List<bool>);
            }
        }

        private void init()
        {
            string subIp = "127.0.0.1";//从机IP
            string subPart = "7373";//从机端口号
            rs = new RemoteServer(subIp, int.Parse(subPart));
            rs.StartServer();
            Thread.Sleep(1000);
            RemoteServer.ErrorType serviceErrType = rs.StartServerStatus;
            if (serviceErrType == RemoteServer.ErrorType.ConnectError)
            {
                ShowLog("错误信息：从机服务启动失败！请检查参数配置是否正确后，重新启动！");
                return;
            }
            else
            {
                ShowLog("信息：从机服务启动完成！");
                ShowLog("信息：初始化从机功能......");
                initFunction();//初始化从机功能
                ShowLog("信息：初始化从机功能完成！");
            }
        }

        public void ShowLog(string dd)
        {
            Console.WriteLine(dd);

        }

        private void initFunction()
        {
            rs.InspectionInstance.OpenApplicationEvent += OpenApplication;
            rs.InspectionInstance.CloseApplicationEvent += CloseApplication;
            rs.InspectionInstance.SendCommandEvent += apiApp.SendCommand;
            rs.InspectionInstance.StartEvent += apiApp.Start;
        }

        private void CloseApplication()
        {
            st = new StartApp(() =>
            {
                apiApp.Hide();
                apiApp.Dispose();
            });
            Task t = new Task(() =>
            {
                //this.BeginInvoke(st);
                apiApp.Hide();
                apiApp.Dispose();
            });
            t.Start();
        }

        private void OpenApplication()
        {
            try
            {
                //st = new StartApp(() => { apiApp.Show(); });
                Task t = new Task(() =>
                {
                    //this.BeginInvoke(st);
                    apiApp.Show();
                });
                t.Start();
            }
            catch (Exception ex)
            {
                ShowLog(ex.TargetSite + ex.Message);
            }
        }

        public delegate void StartApp();
        public StartApp st = null;

    }
}
